#pragma once

#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <thread>
#include <mutex>

namespace My_local_lock
{
    struct LocalLockCpp
    {
        std::mutex *_mymutex;
        LocalLockCpp(std::mutex *mymutex)
            : _mymutex(mymutex)
        {
        }
        ~LocalLockCpp()
        {
        }
        void Lock()
        {
            // std::cout << "lock" << std::endl;
            _mymutex->lock();
        }
        void Unlock()
        {
            // std::cout << "unlock" << std::endl;
            _mymutex->unlock();
        }
    };
    struct LocalLockLinux
    {
        pthread_mutex_t _mymutex;
        LocalLockLinux()
        {
            pthread_mutex_init(&_mymutex, NULL);
        }
        ~LocalLockLinux()
        {
            pthread_mutex_destroy(&_mymutex);
        }
        void Lock()
        {
            pthread_mutex_lock(&_mymutex);
        }
        void Unlock()
        {
            pthread_mutex_unlock(&_mymutex);
        }
    };

    struct LockGuard
    {
        LocalLockLinux &_lockgiard;
        LockGuard(LocalLockLinux &lockgiard)
            : _lockgiard(lockgiard)
        {
            _lockgiard.Lock();
        }
        ~LockGuard()
        {
            _lockgiard.Unlock();
        }
    };
}
